package com.example.sdemo.api.controller;

import com.example.sdemo.app.service.StudentService;
import com.example.sdemo.domain.entity.Score;
import com.example.sdemo.domain.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Resource
    StudentService studentService;
    @RequestMapping("/toAddStudent")
    public String toAddStudent(){
        return "addStudent";
    }
    @PostMapping("/add")
    public String add(/*@RequestBody*/ Student student){
         studentService.insert(student);
        return "redirect:/student/studentList";
    }
    @RequestMapping("/deleteStudent/{id}")
    public String del(@PathVariable Integer id){
         studentService.delete(id);
        return "redirect:/student/studentList";
    }
    @RequestMapping("/toUpdateStudent")
    public String toUpdateStudent(Integer id, Model model){
        Student student = studentService.selectById(id);
        model.addAttribute("student",student);
        return "updateStudent";
    }
    @PostMapping("/update")
    public String update(/*@RequestBody*/ Student student){
        studentService.update(student);
        return "redirect:/student/studentList";
    }
    @PostMapping("/find/{id}")
    public String find(@PathVariable Integer id){
        return studentService.selectById(id).toString();
    }

    /**
     * 查询某一学生所有课程的成绩
     * @param id
     * @return
     */
    @PostMapping("/findScoreByStudentId/{id}")
    public String findScoreOfStudent(@PathVariable Integer id){
        return studentService.selectScoreByStudentId(id).toString();
    }
    /**
     * 查询某一学生所有课程
     * @param id
     * @return
     */
    @PostMapping("/findCourseByStudentId/{id}")
    public String findCourseOfStudent(@PathVariable Integer id){
        return studentService.selectCourseByStudentId(id).toString();
    }
    @GetMapping("/selectALl")
    public List<Student> selectAll(){
        return studentService.selectAll();
    }
    @GetMapping("/studentList")
    public ModelAndView studentPage(){
        ModelAndView modelAndView = new ModelAndView();
        List<Student> students = studentService.selectAll();
        modelAndView.addObject("students",students);
        modelAndView.setViewName("studentList");
        return  modelAndView;
    }
}
